#pragma once
#include <iostream>
#include <mutex>
#include "shared_ptr.hpp"


template<class T>
class weak_ptr
{
public:
    weak_ptr()
    {}


    T& operator*()
    {
        return *_ptr;
    }

    T* operator->()
    {
        return _ptr;
    }

    T*& get()
    {
        return _ptr;
    }

    // 赋值，资源只能被一个shared_ptr指向，p2 = p1
    weak_ptr<T>& operator=(shared_ptr<T>& p1)
    {
        std::cout << "operator=" << std::endl;
        
        // ptr指向p1资源
        _ptr = p1.get();
        return *this;
    }

    // 拷贝构造p2(p1)
    weak_ptr(shared_ptr<T>& p1)
    :_ptr(p1.get())
    {
        std::cout << "weak_ptr(shared_ptr<T>& p1)" << std::endl;
    }

private:
    T* _ptr; // 指针
};